#include "ChordLib.h"
#include<pthread.h>

/*
#define MAX 300
int Number = 300;
nt KEYS[300]= {
120,727,567,593,990,852,16,313,411,230,233,945,221,826,515,661,205,534,736,62,965,255,860,520,985,419,876,449,571,68,27,691,795,595,261,761,423,277,51,834,508,284,755,729,87,246,366,292,780,79,354,722,334,191,218,296
,610,71,745,157,139,773,849,935,344,86,672,767,363,723,577,871,1008,309,576,71,555,943,363,312,1022,717,10,332,908,228,628,494,299,350,652,439,99,477,350,443,563,1022,186,926,722,763,774,706,48,326,777,604,245,116,916
,243,833,926,576,718,130,180,188,430,530,840,869,629,293,195,48,856,193,234,759,915,998,509,597,22,835,350,626,57,466,518,300,276,420,876,994,551,33,158,981,563,999,826,169,268,1021,217,101,190,452,860,82,426,345,679,
448,156,6,51,213,472,569,514,748,990,366,718,517,399,877,474,963,852,276,108,96,273,325,197,463,777,33,545,179,378,201,628,535,207,679,748,679,224,238,404,190,605,98,707,1004,975,157,943,803,433,27,900,706,353,73,146,
106,107,691,286,485,892,914,1020,75,569,745,755,793,983,135,984,564,233,667,545,185,825,464,988,234,492,864,941,845,938,63,951,21,754,213,506,623,103,503,698,672,224,429,442,183,564,402,748,798,45,269,983,870,733,947
,81,201,788,1022,22,702,61,974,723,815,163,205,414,267,708,89,939,932,518,357,92,59,759,
};*/ 
/*
int KEYS[400]= {
888,767,872,276,903,378,818,576,813,498,941,406,738,805,992,483,288,890,1022,680,175,966,613,336,969,290,555,917,717,78,976,582,846,824,858,725,178,653,277,992,127,195,374,865,1000,342,325,264,208,323,944,383,265,533,719
,210,824,250,103,517,329,55,75,151,880,934,876,34,563,129,2,690,324,377,532,301,719,857,565,928,156,486,287,421,1019,1007,631,819,233,734,313,562,789,388,713,645,298,565,680,861,695,682,528,1019,35,36,296,755,893,862,659,
25,324,946,446,319,929,53,115,139,787,428,701,552,816,391,174,91,956,854,952,627,512,456,623,548,492,919,279,361,757,938,386,57,860,832,377,766,885,492,905,648,920,582,177,712,973,351,803,906,181,732,509,693,164,108,217,657
,4,496,1018,761,410,381,819,247,189,172,1013,51,664,894,699,560,452,876,248,402,203,28,284,384,760,793,54,924,902,271,557,906,768,552,643,154,933,438,401,98,610,390,149,250,260,849,810,713,701,35,91,905,63,375,265,823,144,319
,723,22,591,257,928,335,809,548,489,718,986,891,816,573,257,966,823,518,791,610,207,468,645,298,349,708,673,615,507,817,934,206,840,501,463,744,836,248,268,302,966,231,169,759,804,426,701,603,944,468,189,127,936,834,425,262,
518,74,877,1,892,787,208,708,265,671,428,77,920,697,379,862,928,548,597,708,975,274,287,895,742,477,1023,655,10,424,917,806,499,770,807,367,533,1015,51,798,663,479,876,559,152,231,397,56,780,995,764,731,245,28,602,988,505,601
,619,792,2,512,574,501,258,358,868,791,349,919,566,1012,374,418,547,527,649,945,583,405,916,324,112,137,352,715,101,857,292,720,625,294,432,176,795,466,534,639,234,883,534,800,872,909,194,395,412,843,316,995,225,222,295,337,346,
647,28,447};*/ 
/*

int KEYS[500]= {
471,866,846,819,251,687,557,79,189,136,267,55,834,608,895,371,113,257,217,401,175,268,634,810,338,468,617,430,345,582,437,816,424,260,611,676,947,141,755,112,280,1022,167,91,606,39,462,719,296,680,97,471,948,731,258,262,175,875,
692,520,434,106,312,858,366,924,510,289,44,241,402,325,239,569,416,846,608,878,541,904,534,638,352,458,346,610,720,521,461,389,18,895,495,330,730,861,230,216,126,275,458,528,600,697,74,1016,519,682,870,37,563,381,675,915,839,1021,
501,536,519,962,925,537,834,396,867,540,233,74,756,359,349,190,888,949,888,962,941,383,620,787,420,159,144,72,50,984,69,551,496,588,490,397,101,300,793,969,840,2,19,572,361,368,763,225,293,627,163,210,1010,784,997,407,943,118,479,
994,78,548,521,574,113,1011,971,214,287,740,159,103,742,178,676,79,546,415,305,839,18,468,25,4,228,1023,411,148,117,890,118,195,415,639,769,528,627,716,742,914,432,902,1018,150,56,670,229,603,61,534,418,79,1003,444,83,207,443,495,
355,560,361,473,755,776,89,500,280,716,192,1023,606,624,901,601,774,957,246,1003,536,307,514,955,386,493,375,470,700,818,965,32,354,302,505,85,55,594,585,335,286,777,334,893,377,211,469,127,145,716,106,681,1023,620,612,386,89,987,
856,790,781,797,822,111,75,303,196,130,898,781,466,160,534,800,29,911,1012,499,14,133,191,121,814,190,741,403,576,831,366,408,597,124,181,395,235,257,698,432,387,572,189,853,733,724,630,762,611,618,237,626,751,428,747,541,619,464,
944,171,271,287,580,868,411,761,239,646,1018,938,54,382,486,244,211,195,968,841,958,555,435,171,157,162,600,904,704,195,345,624,366,616,911,946,461,298,684,700,945,678,614,999,36,77,219,248,272,163,65,206,719,501,378,876,663,978,757,
343,149,78,968,515,694,855,438,131,130,98,832,51,776,422,26,813,499,246,37,772,409,102,978,104,603,332,981,243,286,714,586,435,792,530,951,462,362,365,594,492,463,402,543,215,824,569,4,300,815,41,48,201,144,2,305,747,335,262,990,621,
976,553,33,744,59,984,183,421,325,777,913,788,155,432,1003,979,1002,1008,255,793,25,303,994,169,306,276,917,641,538,883
};
*/

/*
int KEYS[600] = {
927,50,70,652,816,172,623,525,262,665,136,324,483,633,703,253,414,151,219,30,5,943,248,658,747,244,372,249,335,683,225,238,733,295,890,525,468,489,27,730,130,163,31,614,797,734,867,187,886,62,217,891,1005,465,526,729,709,898,978,20,557,179,258,266,474,125,792,942,614,819,649,745,982,680,335,755,390,178,942,252,240,135,120,222,600,646,951,285,520,905,305,54,60,564,320,534,689,88,453,279,907,78,0,866,758,335,597,124,513,516,377,754,651,497,976,228,119,903,513,639,784,819,693,844,359,1014,354,24,78,807,303,986,885,304,828,619,639,401,744,129,917,97,883,545,594,835,773,713,714,262,328,474,57,1022,294,416,1012,648,440,66,432,744,28,293,24,856,913,663,234,633,792,127,730,651,672,300,462,421,1013,152,684,317,626,741,315,920,134,303,545,574,370,977,294,398,246,318,231,135,982,465,768,750,592,474,378,241,774,840,662,763,993,322,57,595,40,372,492,174,676,13,748,22,990,19,420,212,337,651,348,295,92,92,22,685,567,400,926,317,216,564,57,185,887,114,781,927,486,249,77,138,262,825,160,228,844,581,440,158,208,788,453,301,881,475,986,424,875,888,741,68,428,798,253,291,912,10,194,375,259,271,513,521,73,674,749,917,231,166,51,439,954,505,740,811,980,702,211,832,566,953,900,995,727,129,262,616,140,457,991,399,728,480,921,801,130,646,695,361,812,746,801,743,227,517,530,184,196,742,1016,762,671,892,733,374,1021,996,990,137,429,957,537,133,414,434,935,544,56,606,906,869,328,683,588,556,176,94,740,372,836,732,111,483,600,844,858,597,816,824,735,221,758,248,355,148,682,266,692,738,872,574,583,176,233,147,732,410,242,448,782,54,156,893,538,756,714,372,330,506,172,41,728,930,289,59,54,971,325,747,685,177083,297,245,349,531,392,58,941,634,506,699,689,663,569,203,395,259,575,725,765,747,766,469,654,31,528,708,1002,853,431,664,2,729,909,352,236,277,410,153,912,916,852,577,555,397,780,951,656,331,652,398,54,395,867,708,426,372,393,405,201,824,45,204,529,954,556,765,207,966,918,95,858,747,672,390,120,428,317,777,759,969,151,814,340,1018,498,767,366,891,148,568,692,193,772,197,123,304,963,330,246,857,426,80,580,74,470,701,503,787,454,238,733,605,28,49,599,527,816,966,394,964,510,62,133,258,260,256,562,199,587,808,32,1013,888,613,63,335,290,566,98,744,805,831,325,833,881,924,336,673,866,731,614,352,793,747,610,29,1004,148,228,567,956,261,556,821,874,619,132,140,162,230,884,967,38,185,776,919,85,89,568,952,820,158,280,589,906,891,619,886,15,847};*/
/*
int KEYS[700] = {
372,768,70,953,201,203,997,484,291,6,139,594,596,710,318,641,193,476,904,86,146,315,148,256,930,340,627,787,934,384,77,282,128,14,212,329,351,185,813,643,807,953,213,379,639,531,1020,832,1008,901,918,130,192,43,387,98,383,1014,885,
293,374,963,575,502,87,787,831,438,972,621,57,755,550,270,10,165,802,107,997,786,1008,892,916,176,935,279,274,294,269,135,587,643,74,138,121,161,926,953,600,874,550,657,606,76,928,716,241,706,823,214,468,807,82,360,983,1017,640,233,287,
909,369,874,2,443,1013,650,605,915,579,181,765,105,838,347,181,742,40,422,424,863,637,892,647,719,229,606,713,869,840,1000,754,185,51,259,628,840,910,209,731,465,390,472,571,205,820,752,947,860,151,348,699,788,216,322,483,445,929,172,
290,745,149,21,930,1000,280,534,816,16,744,523,632,110,995,179,315,791,931,239,627,58,587,303,846,803,625,306,225,530,478,515,251,627,536,157,603,817,692,395,983,412,918,591,522,890,770,838,657,678,53,261,736,640,564,559,419,165,865,644,
696,319,136,947,947,672,81,526,465,773,922,425,161,86,1016,683,682,7,497,316,417,550,577,129,66,117,688,586,282,59,206,978,849,342,902,772,1015,983,274,456,732,172,881,893,989,874,552,647,613,26,963,6,576,516,135,743,633,824,305,916,
329,511,80,154,854,748,926,845,707,177,277,415,349,135,284,314,1009,837,962,598,863,901,604,415,394,739,134,3,539,439,919,869,951,766,1023,781,490,926,602,174,79,879,589,428,1014,874,743,999,687,681,53,526,558,153,941,952,893,52,956,408,
491,1,3,418,593,253,175,60,155,777,234,234,633,823,662,623,673,381,599,336,38,18,862,597,302,780,525,171,832,457,579,299,285,833,718,878,62,893,938,217,647,48,451,256,972,89,879,621,471,454,958,509,603,796,82,905,552,608,52,360,
41,631,660,326,440,354,181,502,223,95,719,87,244,146,102,192,236,982,813,707,412,747,192,1015,520,275,896,48,883,948,409,924,556,45,227,996,399,408,475,22,503,170,469,747,317,571,939,553,59,729,236,942,452,428,933,972,703,806,1021,
562,730,406,463,262,451,690,235,850,74,710,448,577,880,917,301,173,465,216,726,994,945,962,912,374,367,822,322,46,604,319,609,310,725,48,53,152,738,808,1002,812,494,427,365,350,320,666,524,785,883,226,756,804,165,644,154,532,442,477,
578,22,796,163,333,498,211,906,650,949,690,629,737,160,32,79,510,352,745,10,114,604,237,70,385,402,490,539,934,933,1016,488,955,789,652,264,263,863,146,913,789,836,518,502,996,550,581,483,903,303,493,1017,907,730,863,268,108,329,808,
18,238,800,507,170,565,135,434,828,998,581,718,73,393,22,242,366,763,823,849,642,102,318,635,1010,25,474,254,133,803,38,152,18,839,659,188,380,794,62,185,768,179,903,508,573,91,750,939,854,549,764,472,652,58,83,638,83,557,892,217,
337,931,369,355,746,4,543,102,798,141,287,542,321,166,26,894,258,776,809,88,302,549,561,954,607,644,568,691,178,436,908,515,343,23,870,65,257,389,168,31,530,455,7,81,622};
*/

/*
int KEYS[800] = {
1,1022,3,1020,5,1018,7,1016,9,1014,11,1012,13,1010,15,1008,17,1006,19,1004,21,1002,23,1000,25,998,27,996,29,994,31,992,33,990,35,988,37,986,39,984,41,982,43,980,45,978,47,976,49,974,51,972,53,970,55,968,57,966,59,964,61,962,63,960,65,
958,67,956,69,954,71,952,73,950,75,948,77,946,79,944,81,942,83,940,85,938,87,936,89,934,91,932,93,930,95,928,97,926,99,924,101,922,103,920,105,918,107,916,109,914,111,912,113,910,115,908,117,906,119,904,121,902,123,900,125,898,127,896,
129,894,131,892,133,890,135,888,137,886,139,884,141,882,143,880,145,878,147,876,149,874,151,872,153,870,155,868,157,866,159,864,161,862,163,860,165,858,167,856,169,854,171,852,173,850,175,848,177,846,179,844,181,842,183,840,185,838,187,
836,189,834,191,832,193,830,195,828,197,826,199,824,201,822,203,820,205,818,207,816,209,814,211,812,213,810,215,808,217,806,219,804,221,802,223,800,225,798,227,796,229,794,231,792,233,790,235,788,237,786,239,784,241,782,243,780,245,778,
247,776,249,774,251,772,253,770,255,768,257,766,259,764,261,762,263,760,265,758,267,756,269,754,271,752,273,750,275,748,277,746,279,744,281,742,283,740,285,738,287,736,289,734,291,732,293,730,295,728,297,726,299,724,301,722,303,720,305,
718,307,716,309,714,311,712,313,710,315,708,317,706,319,704,321,702,323,700,325,698,327,696,329,694,331,692,333,690,335,688,337,686,339,684,341,682,343,680,345,678,347,676,349,674,351,672,353,670,355,668,357,666,359,664,361,662,363,660,
365,658,367,656,369,654,371,652,373,650,375,648,377,646,379,644,381,642,383,640,385,638,387,636,389,634,391,632,393,630,395,628,397,626,399,624,401,622,403,620,405,618,407,616,409,614,411,612,413,610,415,608,417,606,419,604,421,602,423,
600,425,598,427,596,429,594,431,592,433,590,435,588,437,586,439,584,441,582,443,580,445,578,447,576,449,574,451,572,453,570,455,568,457,566,459,564,461,562,463,560,465,558,467,556,469,554,471,552,473,550,475,548,477,546,479,544,481,542,
483,540,485,538,487,536,489,534,491,532,493,530,495,528,497,526,499,524,501,522,503,520,505,518,507,516,509,514,511,512,513,510,515,508,517,506,519,504,521,502,523,500,525,498,527,496,529,494,531,492,533,490,535,488,537,486,539,484,541,
482,543,480,545,478,547,476,549,474,551,472,553,470,555,468,557,466,559,464,561,462,563,460,565,458,567,456,569,454,571,452,573,450,575,448,577,446,579,444,581,442,583,440,585,438,587,436,589,434,591,432,593,430,595,428,597,426,599,424,
601,422,603,420,605,418,607,416,609,414,611,412,613,410,615,408,617,406,619,404,621,402,623,400,625,398,627,396,629,394,631,392,633,390,635,388,637,386,639,384,641,382,643,380,645,378,647,376,649,374,651,372,653,370,655,368,657,366,659,
364,661,362,663,360,665,358,667,356,669,354,671,352,673,350,675,348,677,346,679,344,681,342,683,340,685,338,687,336,689,334,691,332,693,330,695,328,697,326,699,324,701,322,703,320,705,318,707,316,709,314,711,312,713,310,715,308,717,306,
719,304,721,302,723,300,725,298,727,296,729,294,731,292,733,290,735,288,737,286,739,284,741,282,743,280,745,278,747,276,749,274,751,272,753,270,755,268,757,266,759,264,761,262,763,260,765,258,767,256,769,254,771,252,773,250,775,248,777,
246,779,244,781,242,783,240,785,238,787,236,789,234,791,232,793,230,795,228,797,226,799,224};
*/
/*
int KEYS[900]= {
1,1022,3,1020,5,1018,7,1016,9,1014,11,1012,13,1010,15,1008,17,1006,19,1004,21,1002,23,1000,25,998,27,996,29,994,31,992,33,990,35,988,37,986,39,984,41,982,43,980,45,978,47,976,49,974,51,972,53,970,55,968,57,966,59,964,61,962,63,960,65,
958,67,956,69,954,71,952,73,950,75,948,77,946,79,944,81,942,83,940,85,938,87,936,89,934,91,932,93,930,95,928,97,926,99,924,101,922,103,920,105,918,107,916,109,914,111,912,113,910,115,908,117,906,119,904,121,902,123,900,125,898,127,896,
129,894,131,892,133,890,135,888,137,886,139,884,141,882,143,880,145,878,147,876,149,874,151,872,153,870,155,868,157,866,159,864,161,862,163,860,165,858,167,856,169,854,171,852,173,850,175,848,177,846,179,844,181,842,183,840,185,838,187,
836,189,834,191,832,193,830,195,828,197,826,199,824,201,822,203,820,205,818,207,816,209,814,211,812,213,810,215,808,217,806,219,804,221,802,223,800,225,798,227,796,229,794,231,792,233,790,235,788,237,786,239,784,241,782,243,780,245,778,
247,776,249,774,251,772,253,770,255,768,257,766,259,764,261,762,263,760,265,758,267,756,269,754,271,752,273,750,275,748,277,746,279,744,281,742,283,740,285,738,287,736,289,734,291,732,293,730,295,728,297,726,299,724,301,722,303,720,305,
718,307,716,309,714,311,712,313,710,315,708,317,706,319,704,321,702,323,700,325,698,327,696,329,694,331,692,333,690,335,688,337,686,339,684,341,682,343,680,345,678,347,676,349,674,351,672,353,670,355,668,357,666,359,664,361,662,363,660,
365,658,367,656,369,654,371,652,373,650,375,648,377,646,379,644,381,642,383,640,385,638,387,636,389,634,391,632,393,630,395,628,397,626,399,624,401,622,403,620,405,618,407,616,409,614,411,612,413,610,415,608,417,606,419,604,421,602,423,
600,425,598,427,596,429,594,431,592,433,590,435,588,437,586,439,584,441,582,443,580,445,578,447,576,449,574,451,572,453,570,455,568,457,566,459,564,461,562,463,560,465,558,467,556,469,554,471,552,473,550,475,548,477,546,479,544,481,542,
483,540,485,538,487,536,489,534,491,532,493,530,495,528,497,526,499,524,501,522,503,520,505,518,507,516,509,514,511,512,513,510,515,508,517,506,519,504,521,502,523,500,525,498,527,496,529,494,531,492,533,490,535,488,537,486,539,484,541,
482,543,480,545,478,547,476,549,474,551,472,553,470,555,468,557,466,559,464,561,462,563,460,565,458,567,456,569,454,571,452,573,450,575,448,577,446,579,444,581,442,583,440,585,438,587,436,589,434,591,432,593,430,595,428,597,426,599,424,
601,422,603,420,605,418,607,416,609,414,611,412,613,410,615,408,617,406,619,404,621,402,623,400,625,398,627,396,629,394,631,392,633,390,635,388,637,386,639,384,641,382,643,380,645,378,647,376,649,374,651,372,653,370,655,368,657,366,659,
364,661,362,663,360,665,358,667,356,669,354,671,352,673,350,675,348,677,346,679,344,681,342,683,340,685,338,687,336,689,334,691,332,693,330,695,328,697,326,699,324,701,322,703,320,705,318,707,316,709,314,711,312,713,310,715,308,717,306,
719,304,721,302,723,300,725,298,727,296,729,294,731,292,733,290,735,288,737,286,739,284,741,282,743,280,745,278,747,276,749,274,751,272,753,270,755,268,757,266,759,264,761,262,763,260,765,258,767,256,769,254,771,252,773,250,775,248,777,246,779,244,781,242,783,240,785,238,787,236,789,234,791,232,793,230,795,228,797,226,799,224,801,222,803,220,805,218,807,216,809,214,811,212,813,210,815,208,817,206,819,204,821,202,823,200,825,198,827,196,829,194,831,192,833,190,835,188,
837,186,839,184,841,182,843,180,845,178,847,176,849,174,851,172,853,170,855,168,857,166,859,164,861,162,863,160,865,158,867,156,869,154,871,152,873,150,875,148,877,146,879,144,881,142,883,140,885,138,887,136,889,134,891,132,893,130,895,
128,897,126,899,124 }; */

/*int KEYS[1000]= {
1,1022,3,1020,5,1018,7,1016,9,1014,11,1012,13,1010,15,1008,17,1006,19,1004,21,1002,23,1000,25,998,27,996,29,994,31,992,33,990,35,988,37,986,39,984,41,982,43,980,45,978,47,976,49,974,51,972,53,970,55,968,57,966,59,964,61,962,63,960,65,
958,67,956,69,954,71,952,73,950,75,948,77,946,79,944,81,942,83,940,85,938,87,936,89,934,91,932,93,930,95,928,97,926,99,924,101,922,103,920,105,918,107,916,109,914,111,912,113,910,115,908,117,906,119,904,121,902,123,900,125,898,127,896,
129,894,131,892,133,890,135,888,137,886,139,884,141,882,143,880,145,878,147,876,149,874,151,872,153,870,155,868,157,866,159,864,161,862,163,860,165,858,167,856,169,854,171,852,173,850,175,848,177,846,179,844,181,842,183,840,185,838,187,
836,189,834,191,832,193,830,195,828,197,826,199,824,201,822,203,820,205,818,207,816,209,814,211,812,213,810,215,808,217,806,219,804,221,802,223,800,225,798,227,796,229,794,231,792,233,790,235,788,237,786,239,784,241,782,243,780,245,778,
247,776,249,774,251,772,253,770,255,768,257,766,259,764,261,762,263,760,265,758,267,756,269,754,271,752,273,750,275,748,277,746,279,744,281,742,283,740,285,738,287,736,289,734,291,732,293,730,295,728,297,726,299,724,301,722,303,720,305,
718,307,716,309,714,311,712,313,710,315,708,317,706,319,704,321,702,323,700,325,698,327,696,329,694,331,692,333,690,335,688,337,686,339,684,341,682,343,680,345,678,347,676,349,674,351,672,353,670,355,668,357,666,359,664,361,662,363,660,
365,658,367,656,369,654,371,652,373,650,375,648,377,646,379,644,381,642,383,640,385,638,387,636,389,634,391,632,393,630,395,628,397,626,399,624,401,622,403,620,405,618,407,616,409,614,411,612,413,610,415,608,417,606,419,604,421,602,423,
600,425,598,427,596,429,594,431,592,433,590,435,588,437,586,439,584,441,582,443,580,445,578,447,576,449,574,451,572,453,570,455,568,457,566,459,564,461,562,463,560,465,558,467,556,469,554,471,552,473,550,475,548,477,546,479,544,481,542,
483,540,485,538,487,536,489,534,491,532,493,530,495,528,497,526,499,524,501,522,503,520,505,518,507,516,509,514,511,512,513,510,515,508,517,506,519,504,521,502,523,500,525,498,527,496,529,494,531,492,533,490,535,488,537,486,539,484,541,
482,543,480,545,478,547,476,549,474,551,472,553,470,555,468,557,466,559,464,561,462,563,460,565,458,567,456,569,454,571,452,573,450,575,448,577,446,579,444,581,442,583,440,585,438,587,436,589,434,591,432,593,430,595,428,597,426,599,424,
601,422,603,420,605,418,607,416,609,414,611,412,613,410,615,408,617,406,619,404,621,402,623,400,625,398,627,396,629,394,631,392,633,390,635,388,637,386,639,384,641,382,643,380,645,378,647,376,649,374,651,372,653,370,655,368,657,366,659,
364,661,362,663,360,665,358,667,356,669,354,671,352,673,350,675,348,677,346,679,344,681,342,683,340,685,338,687,336,689,334,691,332,693,330,695,328,697,326,699,324,701,322,703,320,705,318,707,316,709,314,711,312,713,310,715,308,717,306,
719,304,721,302,723,300,725,298,727,296,729,294,731,292,733,290,735,288,737,286,739,284,741,282,743,280,745,278,747,276,749,274,751,272,753,270,755,268,757,266,759,264,761,262,763,260,765,258,767,256,769,254,771,252,773,250,775,248,777,
246,779,244,781,242,783,240,785,238,787,236,789,234,791,232,793,230,795,228,797,226,799,224,801,222,803,220,805,218,807,216,809,214,811,212,813,210,815,208,817,206,819,204,821,202,823,200,825,198,827,196,829,194,831,192,833,190,835,188,
837,186,839,184,841,182,843,180,845,178,847,176,849,174,851,172,853,170,855,168,857,166,859,164,861,162,863,160,865,158,867,156,869,154,871,152,873,150,875,148,877,146,879,144,881,142,883,140,885,138,887,136,889,134,891,132,893,130,895,
128,897,126,899,124,901,122,903,120,905,118,907,116,909,114,911,112,913,110,915,108,917,106,919,104,921,102,923,100,925,98,927,96,929,94,931,92,933,90,935,88,937,86,939,84,941,82,943,80,945,78,947,76,949,74,951,72,953,70,955,68,957,66,
959,64,961,62,963,60,965,58,967,56,969,54,971,52,973,50,975,48,977,46,979,44,981,42,983,40,985,38,987,36,989,34,991,32,993,30,995,28,997,26,999,24
};
*/
/*int KEYS1[1600]={ 927,50,70,652,816,172,623,525,262,665,136,324,483,633,703,253,414,151,219,30,5,943,248,658,747,244,372,249,335,683,225,238,733,295,890,525,468,489,27,730,130,163,31,614,797,734,867,187,886,62,217,891,1005,465,526,729,709,898,978,20,
557,179,258,266,474,125,792,942,614,819,649,745,982,680,335,755,390,178,942,252,240,135,120,222,600,646,951,285,520,905,305,54,60,564,320,534,689,88,453,279,907,78,0,866,758,335,597,124,513,516,377,754,651,497,976,228,119,903,513,639
,784,819,693,844,359,1014,354,24,78,807,303,986,885,304,828,619,639,401,744,129,917,97,883,545,594,835,773,713,714,262,328,474,57,1022,294,416,1012,648,440,66,432,744,28,293,24,856,913,663,234,633,792,127,730,651,672,300,462,421,1013,
152,684,317,626,741,315,920,134,303,545,574,370,977,294,398,246,318,231,135,982,465,768,750,592,474,378,241,774,840,662,763,993,322,57,595,40,372,492,174,676,13,748,22,990,19,420,212,337,651,348,295,92,92,22,685,567,400,926,317,216,564,
57,185,887,114,781,927,486,249,77,138,262,825,160,228,844,581,440,158,208,788,453,301,881,475,986,424,875,888,741,68,428,798,253,291,912,10,194,375,259,271,513,521,73,674,749,917,231,166,51,439,954,505,740,811,980,702,211,832,566,953,900,
995,727,129,262,616,140,457,991,399,728,480,921,801,130,646,695,361,812,746,801,743,227,517,530,184,196,742,1016,762,671,892,733,374,1021,996,990,137,429,957,537,133,414,434,935,544,56,606,906,869,328,683,588,556,176,94,740,372,836,732,
111,483,600,844,858,597,816,824,735,221,758,248,355,148,682,266,692,738,872,574,583,176,233,147,732,410,242,448,782,54,156,893,538,756,714,372,330,506,172,41,728,930,289,59,54,971,325,747,685,177083,297,245,349,531,392,58,941,634,506,699,689
,663,569,203,395,259,575,725,765,747,766,469,654,31,528,708,1002,853,431,664,2,729,909,352,236,277,410,153,912,916,852,577,555,397,780,951,656,331,652,398,54,395,867,708,426,372,393,405,201,824,45,204,529,954,556,765,207,966,918,95,858,
747,672,390,120,428,317,777,759,969,151,814,340,1018,498,767,366,891,148,568,692,193,772,197,123,304,963,330,246,857,426,80,580,74,470,701,503,787,454,238,733,605,28,49,599,527,816,966,394,964,510,62,133,258,260,256,562,199,587,808,32,
1013,888,613,63,335,290,566,98,744,805,831,325,833,881,924,336,673,866,731,614,352,793,747,610,29,1004,148,228,567,956,261,556,821,874,619,132,140,162,230,884,967,38,185,776,919,85,89,568,952,820,158,280,589,906,891,619,886,15,847,
1,1022,3,1020,5,1018,7,1016,9,1014,11,1012,13,1010,15,1008,17,1006,19,1004,21,1002,23,1000,25,998,27,996,29,994,31,992,33,990,35,988,37,986,39,984,41,982,43,980,45,978,47,976,49,974,51,972,53,970,55,968,57,966,59,964,61,962,63,960,65,
958,67,956,69,954,71,952,73,950,75,948,77,946,79,944,81,942,83,940,85,938,87,936,89,934,91,932,93,930,95,928,97,926,99,924,101,922,103,920,105,918,107,916,109,914,111,912,113,910,115,908,117,906,119,904,121,902,123,900,125,898,127,896,
129,894,131,892,133,890,135,888,137,886,139,884,141,882,143,880,145,878,147,876,149,874,151,872,153,870,155,868,157,866,159,864,161,862,163,860,165,858,167,856,169,854,171,852,173,850,175,848,177,846,179,844,181,842,183,840,185,838,187,
836,189,834,191,832,193,830,195,828,197,826,199,824,201,822,203,820,205,818,207,816,209,814,211,812,213,810,215,808,217,806,219,804,221,802,223,800,225,798,227,796,229,794,231,792,233,790,235,788,237,786,239,784,241,782,243,780,245,778,
247,776,249,774,251,772,253,770,255,768,257,766,259,764,261,762,263,760,265,758,267,756,269,754,271,752,273,750,275,748,277,746,279,744,281,742,283,740,285,738,287,736,289,734,291,732,293,730,295,728,297,726,299,724,301,722,303,720,305,
718,307,716,309,714,311,712,313,710,315,708,317,706,319,704,321,702,323,700,325,698,327,696,329,694,331,692,333,690,335,688,337,686,339,684,341,682,343,680,345,678,347,676,349,674,351,672,353,670,355,668,357,666,359,664,361,662,363,660,
365,658,367,656,369,654,371,652,373,650,375,648,377,646,379,644,381,642,383,640,385,638,387,636,389,634,391,632,393,630,395,628,397,626,399,624,401,622,403,620,405,618,407,616,409,614,411,612,413,610,415,608,417,606,419,604,421,602,423,
600,425,598,427,596,429,594,431,592,433,590,435,588,437,586,439,584,441,582,443,580,445,578,447,576,449,574,451,572,453,570,455,568,457,566,459,564,461,562,463,560,465,558,467,556,469,554,471,552,473,550,475,548,477,546,479,544,481,542,
483,540,485,538,487,536,489,534,491,532,493,530,495,528,497,526,499,524,501,522,503,520,505,518,507,516,509,514,511,512,513,510,515,508,517,506,519,504,521,502,523,500,525,498,527,496,529,494,531,492,533,490,535,488,537,486,539,484,541,
482,543,480,545,478,547,476,549,474,551,472,553,470,555,468,557,466,559,464,561,462,563,460,565,458,567,456,569,454,571,452,573,450,575,448,577,446,579,444,581,442,583,440,585,438,587,436,589,434,591,432,593,430,595,428,597,426,599,424,
601,422,603,420,605,418,607,416,609,414,611,412,613,410,615,408,617,406,619,404,621,402,623,400,625,398,627,396,629,394,631,392,633,390,635,388,637,386,639,384,641,382,643,380,645,378,647,376,649,374,651,372,653,370,655,368,657,366,659,
364,661,362,663,360,665,358,667,356,669,354,671,352,673,350,675,348,677,346,679,344,681,342,683,340,685,338,687,336,689,334,691,332,693,330,695,328,697,326,699,324,701,322,703,320,705,318,707,316,709,314,711,312,713,310,715,308,717,306,
719,304,721,302,723,300,725,298,727,296,729,294,731,292,733,290,735,288,737,286,739,284,741,282,743,280,745,278,747,276,749,274,751,272,753,270,755,268,757,266,759,264,761,262,763,260,765,258,767,256,769,254,771,252,773,250,775,248,777,
246,779,244,781,242,783,240,785,238,787,236,789,234,791,232,793,230,795,228,797,226,799,224,801,222,803,220,805,218,807,216,809,214,811,212,813,210,815,208,817,206,819,204,821,202,823,200,825,198,827,196,829,194,831,192,833,190,835,188,
837,186,839,184,841,182,843,180,845,178,847,176,849,174,851,172,853,170,855,168,857,166,859,164,861,162,863,160,865,158,867,156,869,154,871,152,873,150,875,148,877,146,879,144,881,142,883,140,885,138,887,136,889,134,891,132,893,130,895,
128,897,126,899,124,901,122,903,120,905,118,907,116,909,114,911,112,913,110,915,108,917,106,919,104,921,102,923,100,925,98,927,96,929,94,931,92,933,90,935,88,937,86,939,84,941,82,943,80,945,78,947,76,949,74,951,72,953,70,955,68,957,66,
959,64,961,62,963,60,965,58,967,56,969,54,971,52,973,50,975,48,977,46,979,44,981,42,983,40,985,38,987,36,989,34,991,32,993,30,995,28,997,26,999,24
};*/
//pthread_mutex_t mutex3 = PTHREAD_MUTEX_INITIALIZER;

/*
int ResponsibleFor() {
      int i=0,cnt=0;
    for(i=0;i<Number;i++) {
                  
               
               if(liesBetween(KEYS[i] , pred->keyID , finger[0]->keyID))
                {
                      printf("\n*******************************************************");
                      printf("\nI am responsible for %d" , KEYS[i]);
                      printf("\n********************************************************");
                cnt++;
                }
	}  
printf("\nThe number of keys I am responsible for is %d",cnt);
    
return 1;

}
*/
/*
void *UnknownID() {
	printf("I am thread UnknownID");
	int i=0,j=0,n;
	int count=0;
	int IDArray[10];
	char *buf;
	struct Node *new;
	int hops;
	printf("\nThe value of finger[0]->keyID before calling lookupCounter(): %d\n",finger[0]->keyID);
	while(i<10)
	{
		
                if( liesBetween(KEYS[count] , pred->keyID,finger[0]->keyID)==1)
		{
		printf("lies between is wrong");	
		
		}else{

				
		IDArray[i]=KEYS[count];
                i++;
		j++;
		printf("thevalueofiis %d",i);
		}	

	count++;
	if(count==MAX){
	j=i;
	break;
	}	
	}
	
	for(i=0;i<j;i++){
	printf("\nThe value of finger[0]->keyID before calling lookupCounter(): %d\n",finger[0]->keyID);
	sleep(1);
	hops=lookupCounter(IDArray[i]);
	printf("\n********The number of counts required for the lookup of ID %d is : %d\n",IDArray[i],hops);
	}
	//ResponsibleFor();
}
*/
//pthread_mutex_t mutex3 = PTHREAD_MUTEX_INITIALIZER;

void *PeerServer(void *newNodeServer ) {

	tcpServer((struct Node *)newNodeServer );
}

void *PeerClient(void *newNode) {
	printf("Peer with server address : %s joining\n",nodeToString((struct Node *)newNode));
	join(((struct Node *)newNode)->ipstr,((struct Node*)newNode)->port);
}


void *getInput() {
        char buf[25];
        int n,i,keyID;
        char id[5];
        while ((n = read(0, buf, 24)) > 0) {
                printf("Read value %s",buf);
                if ( strcmp(buf,"leave\n")==0 ) {
                        printf("The Node %d is leaving\n ",finger[0]->keyID);
                        leave();
                }
                else if(strncmp(buf,"GET",3)==0 ) {
                        for (i=0;i<4;i++) {
                                id[i]=buf[4+i];
                        }
                        id[4]='\0';
                        keyID = atoi(id);
                        printf("GETTING rfc for id  %d\n",keyID);
                        triggerSingleRFC(keyID);

                } 
		//else if (strcmp(buf,"task2\n")==0) {
		//	printf("Calling function UnknownID\n");
 		//	UnknownID();	
		//}
                memset(buf,0,25);

        }
        return 0;


}

void *stabilizeThread() {
//	pthread_mutex_lock( &mutex3 );
	int stabCounter=0;
	while(1) {
		if ( leaveFlag ==1 ) {
			finalStabilizeComplete=1;
			break;
		}
		sleep(2);
		if (stabCounter ==3) {
			getRFCresponsible();
		}
		stabilize();
                fixFingers();

		stabCounter++;
		printTable();

		if (leaveFlag==1) 
			finalStabilizeComplete=1;
//	pthread_mutex_unlock( &mutex3 );
	}
}

int main(int argc, char *argv[])
{
        int i=0;
        char *t;
        struct Node *selfNode;
        pthread_t threadClient,threadServer,threadStabilize,threadGetInput,threadUnknownID;
        selfNode = (struct Node *)malloc(sizeof(struct Node));
        strcpy(selfNode->ipstr,argv[1]);
        selfNode->port = atoi(argv[2]);
        printf("Peer with server address : %s:%s joining\n",argv[1],argv[2]);
        printf ("\n CHORD LIGHT IS NOW LISTENING ");


//        tcpServer();
//        sleep(2);
//        if (!fork()) s{
//                while(1) {
//                        sleep(5);
//                        printf("Stabilize thread\n");
//                        stabilize();
//                }
                        
//        } else {
        pthread_create(&threadServer,NULL,PeerServer,(void *)selfNode);

        pthread_create(&threadClient,NULL,PeerClient,(void *)selfNode);
//        }
        pthread_create(&threadStabilize,NULL,stabilizeThread,NULL);

        pthread_create(&threadGetInput,NULL,getInput,NULL);

        
        pthread_join(threadServer,NULL);        
        pthread_join(threadClient,NULL);
        pthread_join(threadStabilize,NULL);
}


